package com.pi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pi.common.utlis.StringUtils;
import com.pi.common.utlis.page.TableDataInfo;
import com.pi.entity.DataSourceConfig;
import com.pi.model.AjaxResult;
import com.pi.service.DataSourceConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author TomFang
 * @version 1.0
 * @description: 多数据源控制器 d
 * @date 2025/3/26 16:23
 */
@RestController
@RequestMapping("/source")
public class DataSourceConfigController extends BaseController{

    @Autowired
    private DataSourceConfigService dataSourceConfigService;

    /**
     * 获取多数据源配列表
     * @return TableDataInfo
     */
    @GetMapping("/list")
    public TableDataInfo list(DataSourceConfig dataSourceConfig)
    {
        startPage();
        QueryWrapper<DataSourceConfig> queryWrapper = new QueryWrapper<>();
        commonQuery(dataSourceConfig,queryWrapper);
        List<DataSourceConfig> list = dataSourceConfigService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 获取配置的详细信息
     * @param id i
     * @return AjaxResult
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(dataSourceConfigService.getById(id));
    }

    /**
     * 添加数据源
     * @param dataSourceConfig 实体类
     * @return AjaxResult
     */
    @PostMapping
    public AjaxResult add(@RequestBody DataSourceConfig dataSourceConfig) {
        return toAjax(dataSourceConfigService.save(dataSourceConfig));
    }

    /**
     * 编辑数据源
     * @param dataSourceConfig 实体类
     * @return AjaxResult
     */
    @PutMapping
    public AjaxResult edit(@RequestBody DataSourceConfig dataSourceConfig) {
        return toAjax(dataSourceConfigService.updateById(dataSourceConfig));
    }

    /**
     * 删除数据源
     * @param ids 数组
     * @return AjaxResult
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        if(ids != null){
            List<Long> idList = Arrays.asList(ids);
            boolean b = dataSourceConfigService.removeByIds(idList);
            if(b){
                return AjaxResult.success("操作成功！");
            }
            return AjaxResult.error("操作失败！");
        }else {
            return AjaxResult.error("请选择要删除的数据！");
        }
    }

    public void commonQuery(DataSourceConfig DataSourceConfig,QueryWrapper<DataSourceConfig> queryWrapper){
        if(StringUtils.isNotEmpty(DataSourceConfig.getType())){
            queryWrapper.eq("type",DataSourceConfig.getType());    //  数据源来源
        }
        if(StringUtils.isNotEmpty(DataSourceConfig.getDataSource())){
            queryWrapper.eq("data_source",DataSourceConfig.getDataSource());    //  数据源
        }
        if(StringUtils.isNotEmpty(DataSourceConfig.getDataSourceName())){
            queryWrapper.like("data_source_name",DataSourceConfig.getDataSourceName());    //  数据源名称
        }
        if(StringUtils.isNotEmpty(DataSourceConfig.getJdbcUrl())){
            queryWrapper.like("jdbc_url",DataSourceConfig.getJdbcUrl());    //  数据源jdbcUrl
        }
        if(StringUtils.isNotEmpty(DataSourceConfig.getUrl())){
            queryWrapper.like("url",DataSourceConfig.getUrl());    //  请求URL
        }
        if(StringUtils.isNotEmpty(DataSourceConfig.getRequestType())){
            queryWrapper.eq("request_type",DataSourceConfig.getRequestType());    //  请求URL
        }
        queryWrapper.orderByDesc("create_time");
    }
}
